package com.ringtheweb.web.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Conversation;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import com.ringtheweb.agi.services.api.IPlayListCallScheduler;
import com.ringtheweb.model.Play;
import com.ringtheweb.model.PlayList;
import com.ringtheweb.model.User;
import com.ringtheweb.persistence.dao.IPlayListDAO;

@Name("playListAction")
@Scope(ScopeType.CONVERSATION)
public class PlayListAction implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	
	@In FacesMessages facesMessages;
	@Logger	Log LOG;
	
	@In("#{playListDAO}") IPlayListDAO playListDAO;
	
	@In("#{playListCallScheduler}") IPlayListCallScheduler playListCallScheduler;
	
	@In User user;
	
	private String text;
	
	private Date playDate;

	private List<Play> plays = new ArrayList<Play>() ;

	public List<Play> getPlays() {
		return plays;
	}

	public void setPlays(List<Play> plays) {
		this.plays = plays;
	}
	
	public void moveDown(Play play) {
		int playIdx = this.plays.indexOf(play);
		if (++playIdx < this.plays.size()) {
			this.plays.remove(play);
			this.plays.add(playIdx, play);	
		}
	}
	
	public void moveUp(Play play) {
		int playIdx = this.plays.indexOf(play);
		if (--playIdx >= 0) {
			this.plays.remove(play);
			this.plays.add(playIdx, play);	
		}

	}
	
	public void remove(Play play) {
		this.plays.remove(play);
	}
	
	
	public String phone() {
		PlayList playList = new PlayList();
		playList.setPhoneNumber(user.getPhoneNumbers().iterator().next());
		for(Play play : getPlays()) {
			playList.getPlays().add(play);
		}
		PlayList savedPlayList = playListDAO.makePersistent(playList);
		if (playDate != null) {
			playListCallScheduler.schedule(savedPlayList.getId(), playDate);
		}
		else {
			playListCallScheduler.schedule(savedPlayList.getId(), Calendar.getInstance().getTime());
		}
		
		Conversation.instance().end();
		return "";
	}


	public void validatePlayDate(FacesContext facesContext,UIComponent component,Object value) throws
	 	ValidatorException {
		
        Date dateToValidate = (Date)value;

        if (dateToValidate != null) {
        	if (dateToValidate.getTime() < System.currentTimeMillis()) {
        		FacesMessage message = FacesMessages.createFacesMessage(FacesMessage.SEVERITY_ERROR,
        				"error_date","");
                throw new ValidatorException(message);
        	}
        }
	}
	
	
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Date getPlayDate() {
		return playDate;
	}

	public void setPlayDate(Date playDate) {
		this.playDate = playDate;
	}
	
	
	

}
